package com.kzj.fun.container.root;

import com.kzj.common.config.ConfigProperties;
import com.kzj.common.exception.ExceptionEnum;
import com.kzj.common.exception.SystemException;
import com.kzj.common.log.Logger;
import com.kzj.fun.interfaces.MenuBarFunction;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class RootMenuBar extends JMenuBar {
    private int width;
    private int height;
    private MenuBarFunction menuFunction;

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }
    int menuSize=32;
    int border=8;
    public RootMenuBar(MenuBarFunction menuFunction) {
        this.menuFunction=menuFunction;
        JMenu saveMenu=new JMenu();
        JMenu cancelMenu=new JMenu();
        JMenu confirmMenu=new JMenu();
        JMenu paintBrushMenu=new JMenu();
        JMenu lineMenu=new JMenu();
        JMenu rectMenu=new JMenu();
        JMenu fillRectMenu=new JMenu();
        JMenu ellipseMenu=new JMenu();
        JMenu fillEllipseMenu=new JMenu();
        JMenu textMenu=new JMenu();

        Icon saveIcon = getIcon("saveIconPath",menuSize,menuSize);
        Icon cancelIcon = getIcon("cancelIconPath",menuSize,menuSize);
        Icon confirmIcon =getIcon("confirmIconPath",menuSize,menuSize);
        Icon paintBrushIcon=getIcon("paintBrushIconPath",menuSize,menuSize);
        Icon lineIcon=getIcon("lineIconPath",menuSize,menuSize);
        Icon rectIcon=getIcon("rectIconPath",menuSize,menuSize);
        Icon fillRectIcon=getIcon("fillRectIconPath",menuSize,menuSize);
        Icon ellipseIcon=getIcon("ellipseIconPath",menuSize,menuSize);
        Icon fillEllipseIcon=getIcon("fillEllipseIconPath",menuSize,menuSize);
        Icon textIcon=getIcon("textIconPath",menuSize,menuSize);

        saveMenu.setIcon(saveIcon);
        saveMenu.addMouseListener(menuFunction.save());
        cancelMenu.setIcon(cancelIcon);
        cancelMenu.addMouseListener(menuFunction.cancel());
        confirmMenu.setIcon(confirmIcon);
        confirmMenu.addMouseListener(menuFunction.confirm());

        paintBrushMenu.setIcon(paintBrushIcon);
        paintBrushMenu.addMouseListener(menuFunction.paintBrush());
        lineMenu.setIcon(lineIcon);
        lineMenu.addMouseListener(menuFunction.line());
        rectMenu.setIcon(rectIcon);
        rectMenu.addMouseListener(menuFunction.rect());
        fillRectMenu.setIcon(fillRectIcon);
        fillRectMenu.addMouseListener(menuFunction.fillRect());
        ellipseMenu.setIcon(ellipseIcon);
        ellipseMenu.addMouseListener(menuFunction.ellipse());
        fillEllipseMenu.setIcon(fillEllipseIcon);
        fillEllipseMenu.addMouseListener(menuFunction.fillEllipse());
        textMenu.setIcon(textIcon);
        textMenu.addMouseListener(menuFunction.text());



        List<JMenu> list=new ArrayList<>();
        list.add(saveMenu);
        list.add(cancelMenu);
        list.add(confirmMenu);
        list.add(lineMenu);
        list.add(rectMenu);
        list.add(fillRectMenu);
        list.add(ellipseMenu);
        list.add(fillEllipseMenu);
        list.add(paintBrushMenu);
        list.add(textMenu);
        for(int i=0;i<list.size();i++){
            list.get(i).setSize(menuSize,menuSize);
            list.get(i).setBounds((menuSize)*i,0,menuSize,menuSize);
            list.get(i).setBorder(BorderFactory.createEmptyBorder(border,-4,border,border));
            this.add(list.get(i));
        }
        this.width=(menuSize)*list.size();
        this.height=menuSize;
        this.setVisible(true);

    }
    public Icon getIcon(String key,int width,int height){
        String val=ConfigProperties.get(key);
        Image image;
        try{
            image=Toolkit.getDefaultToolkit().getImage(this.getClass().getClassLoader().getResource(val).getPath());
        }catch (Exception e){
            Logger.warn(String.format(" %s 图标加载失败，使用默认图标",key),e);
            try{
                image=Toolkit.getDefaultToolkit().getImage(this.getClass().getClassLoader().getResource(ConfigProperties.mustGet("defaultIconPath")));
            }catch (Exception e1){
                throw new SystemException(ExceptionEnum.RESOURCE_LOAD_FAIL);
            }
        }
        Image scaledInstance = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        return new ImageIcon(scaledInstance);
    }
}
